﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using ObjRec;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;

namespace FaceRecognition
{
    public partial class Form1 : Form
    {
        public static FaceRec _fr = new FaceRec();

        public Form1()
        {
            InitializeComponent();
        }

        private void Capture(object sender, EventArgs e)
        {
            _fr._originalImage = _fr.Stream;
            _fr.Scan();
            camPicture.Image = _fr._originalImage;
            #region Calculate LBP image
            if (_fr._faces != null && _fr._faces.Length > 0)
            {
                histFacePicture.Image = _fr._LBPImg.Bitmap;
                uniformPictureBox.Image = _fr._uniImg.Bitmap;
                nonuniPictureBox.Image = _fr._nonuniImg.Bitmap;
            }
            else
            {
                histFacePicture.Image = null;
                uniformPictureBox.Image = null;
                nonuniPictureBox.Image = null;
            }
            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (btnCapture.Text == "Start capture")
            {
                btnCapture.Text = "Stop capture";
                Application.Idle += Capture;
            }
            else
            {
                StopCapturing();
                camPicture.Image = null;
            }
        }

        private void btnGetFaces_Click(object sender, EventArgs e)
        {
            StopCapturing();
            frmDetected frm = new frmDetected();
            frm.ShowDialog();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                _fr.LoadFile();
                _fr.Train(false);
                _fr.OpenConnection();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void histFacePicture_MouseClick(object sender, MouseEventArgs e)
        {
            PictureBox p = (PictureBox)sender;
            if(e.Button == MouseButtons.Right && p.Image != null)
            {
                StopCapturing();
                frmHistogram frm = new frmHistogram(_fr.GlobalHistogram(_fr._faces[0].Bitmap));
                frm.ShowDialog();
            }
        }

        private void imageSetManagementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StopCapturing();
            frmImageSetManagement frm = new frmImageSetManagement();
            frm.ShowDialog();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = saveFileDialog1.FileName;
                try
                {
                    SaveLoad.Save(filename, _fr._people);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog1.FileName;
                Form1._fr._people = SaveLoad.Load(filename);
                Form1._fr.Train(false);
            }
        }

        private void StopCapturing()
        {
            btnCapture.Text = "Start capture";
            Application.Idle -= Capture;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 frm = new AboutBox1();
            frm.ShowDialog();
        }
    }
}
